package com.filex.ads.banner;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.webkit.WebSettings;

import com.filex.ads.common.logging.Logger;
import com.filex.ads.view.BaseWebView;

public class FilexWebView extends BaseWebView  {

    public FilexWebView(Context context) {
        super(context);

        disableScrollingAndZoom();
        getSettings().setJavaScriptEnabled(true);

        enablePlugins(true);
        setBackgroundColor(Color.TRANSPARENT);
    }

    public void init(HtmlWebViewListener listener) {
        setWebViewClient(new HtmlWebViewClient(listener, this));
    }

    @Override
    public void loadUrl(@Nullable final String url) {
        if (url == null) {
            return;
        }

        if (url.startsWith("javascript:")) {
            super.loadUrl(url);
            return;
        }

        Logger.d("Loading url: " + url);
    }


    public void loadHtmlResponse(String htmlResponse) {
        loadData(htmlResponse,"text/html", "utf-8");
    }

    @Override
    public void stopLoading() {
        if (mIsDestroyed) {
            Logger.w(FilexWebView.class.getSimpleName() + "#stopLoading() called after destroy()");
            return;
        }

        final WebSettings webSettings = getSettings();
        if (webSettings == null) {
            Logger.w(FilexWebView.class.getSimpleName() + "#getSettings() returned null");
            return;
        }

        webSettings.setJavaScriptEnabled(false);
        super.stopLoading();
        webSettings.setJavaScriptEnabled(true);
    }

    private void disableScrollingAndZoom() {
        setHorizontalScrollBarEnabled(false);
        setHorizontalScrollbarOverlay(false);
        setVerticalScrollBarEnabled(false);
        setVerticalScrollbarOverlay(false);
        getSettings().setSupportZoom(false);
    }


}
